import * as webpack from 'webpack';

import * as HtmlWebpackPlugin from 'html-webpack-plugin';
const tsImportPluginFactory = require('ts-import-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const TerserPlugin = require('terser-webpack-plugin');
const OptimizeCSSAssetsPlugin = require('optimize-css-assets-webpack-plugin');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');

import { appMain, appBuild, appTemplate } from "./paths"

import { title } from "./config"

const BaseConfig: webpack.Configuration = {
  entry: {
    bundle: appMain
  },
  stats: {
    children: false,
    modules: false,
  },
  module: {
    rules: [
      {
        test: /\.tsx?$/,
        loader: 'ts-loader',
        options: {
          transpileOnly: true,
          experimentalWatchApi: true,
          getCustomTransformers: () => ({
            before: [tsImportPluginFactory({
              libraryName: 'antd',
              libraryDirectory: 'lib',
              style: 'css'
            })]
          }),
          compilerOptions: {
            module: 'es2015'
          }
        },
        exclude: /node_modules/
      },
      {
        test: /\.(le|c)ss$/,
        use: [
          MiniCssExtractPlugin.loader,
          'css-loader',
          'less-loader',
          {
            loader: require.resolve('postcss-loader'),
            options: {
              plugins: () => [
                require('postcss-flexbugs-fixes'),
                require('autoprefixer'),
              ],
            },
          },
        ]
      },
      {
        test: /\.(svg)$/i,
        loader: 'file-loader',
        options: {
          name: 'static/images/[name].[ext]',
        },
      },
      {
        test: /\.(png|jpg|gif)$/i,
        use: [
          {
            loader: 'url-loader',
            options: {
              limit: 8192
            }
          }
        ]
      },
      {
        loader: 'webpack-ant-icon-loader',
        enforce: 'pre',
        options: {
          chunkName: 'a-icons'
        },
        include: [
          require.resolve('@ant-design/icons/lib/dist')
        ]
      }

    ]
  },
  devtool: "source-map",
  resolve: {
    extensions: [".ts", ".tsx", ".js", ".json"]
  },
  output: {
    path: appBuild,
    filename: 'js/[name].js'
  },
  optimization: {
    minimizer: [
      new TerserPlugin({
        terserOptions: {
          compress: {
            warnings: false,
            drop_console: true,
            drop_debugger: true,
            pure_funcs: ['console.log']
          },
        },
      }),
      new OptimizeCSSAssetsPlugin({})
    ],
    splitChunks: {
      chunks: function (chunk) {
        // 这里的name 可以参考在使用`webpack-ant-icon-loader`时指定的`chunkName`
        return chunk.name !== 'a-icons';
      },
      cacheGroups: {
        rCommon: {
          test: (module) => {
            return /\/react|\/prop-types|\/babel-runtime/.test(module.context);
          }, // 直接使用 test 来做路径匹配，抽离react相关代码
          chunks: "initial",
          name: "rcommon",
          priority: 10,
        },
        aCommon: {
          test: (module) => {
            return /\/antd/.test(module.context);
          }, // 直接使用 test 来做路径匹配，抽离react相关代码
          chunks: "initial",
          name: "acommon",
          priority: 10,
        },
      },
    }
  },
  plugins: [
    new CleanWebpackPlugin(),
    new HtmlWebpackPlugin({
      template: appTemplate,
      title: title,
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        minifyCSS: true
      }
    }),
    new MiniCssExtractPlugin({
      filename: "css/[name].[chunkhash:4].css",
      chunkFilename: "css/[id].[chunkhash:4].css",
      ignoreOrder: false,
    }),
  ]
};

export default BaseConfig;